Repository navigation
fix: send push notifications as SSE events on the SSE transport - #27258
Conversation
Atmosphere frames only writes to a suspended resource, so session expired, UI not found and refresh notifications reached the client as a bare JSON line that EventSource ignores. Fixes #27189
Type of change
How to test
|
| resource.getResponse() | ||
| .setContentType(JsonConstants.JSON_CONTENT_TYPE); | ||
| resource.getResponse().getWriter().write(notificationJson); | ||
| if (resource.transport() == TRANSPORT.SSE |
There was a problem hiding this comment.
Why does this location need to take the transport into account if no other location needs to?
There was a problem hiding this comment.
@Artur- This is the only place that writes straight to the response of a push resource. All other push messages go through the Broadcaster (AtmospherePushConnection.sendMessage) to a suspended resource, and Atmosphere's interceptors add each transport's framing there, including data: for SSE.
Here the resource is usually not suspended yet (the connection is rejected in onConnect or the first receive). On that path:
- WebSocket and long polling need no framing beyond the length prefix, and
TrackMessageSizeInterceptoradds that whether or not the resource is suspended. So the plain JSON write works for them. SSEAtmosphereInterceptoronly writes padding on the first write to an unsuspended resource. It skips thedata:prefix and the closing blank line, soEventSourcedrops the message. That's why only SSE needs its own branch, and only while the resource is unsuspended.
The other option would be to suspend the resource first and broadcast the notification, so Atmosphere frames it for every transport. That would change the disconnect flow for all transports to fix one, so I kept the change local. I can switch to that approach if you prefer it.
There was a problem hiding this comment.
It would sound better not to do low level writes but how large is the blast radius?
There was a problem hiding this comment.
@Artur- Good point. I replaced the low-level write, so the blast radius stays the same as before: only SSE resources that are not suspended yet.
Atmosphere's SSE interceptor already frames writes on an unsuspended resource when the request has the FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL attribute. Atmosphere's JavaScriptProtocol sets it for the handshake of a new connection. So the notification now goes through the normal Atmosphere writer, and PushHandler only sets that attribute first. Atmosphere adds the padding, the data: framing and the message size prefix itself.
I checked the wire output on embedded Jetty with real Atmosphere, using a push request without a session:
- Reconnect with an existing tracking id: before
45|{...sessionExpired...}(unframed), afterdata:45|{...}followed by a blank line. - First connection handshake (tracking id
0):data:45|{...}both before and after, because Atmosphere already set the attribute there.
So the bug only hit reconnects, which is the idle-tab case. WebSocket, long polling and already open SSE connections are untouched. A test now checks that WebSocket requests don't get the attribute.
Instead of writing the event to the container response, mark the request the way Atmosphere marks a connection handshake, so its SSE interceptor adds the framing, padding and length prefix itself.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 2 comments.
| Finding | |
|---|---|
The notification response still declares application/json, which a browser EventSource rejects regardless of the new framing |
|
| 🧪 | The new test asserts that a request attribute was set on a mock, not that the notification is framed as an SSE event |
| FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL, | ||
| Boolean.TRUE); | ||
| } | ||
| resource.getResponse() |
There was a problem hiding this comment.
application/json, which a browser EventSource rejects regardless of the new framing
This path never suspends the resource, and the write sets JsonConstants.JSON_CONTENT_TYPE on the response. The reconnect is a fresh HTTP request whose headers are not committed yet, so the SSE response goes out as application/json; charset=UTF-8. Per the HTML spec, EventSource fails the connection when the content type is not text/event-stream, and the message body is never delivered to the client — the framing fix would then change the wire bytes without fixing the reported symptom.
The establish path sets text/plain the same way and SSE push does work, which implies Atmosphere re-asserts text/event-stream somewhere — most plausibly around suspend, which this path never reaches. Worth confirming in a real browser (check the response headers, not just the body) rather than with a raw HTTP client.
PushHandler.java:671 · correctness
There was a problem hiding this comment.
The response goes out as text/event-stream. On a resource that isn't suspended, the SSE interceptor's first write goes through writePadding, which calls response.setContentType("text/event-stream") and flushes the padding. That happens before any payload, so the JSON content type set here is replaced while the headers are still uncommitted. The curl output in the issue already shows Content-Type: text/event-stream;charset=utf-8 for this path.
I also checked it in a real browser. The new SsePushSessionExpiredIT drops the session cookie and closes the SSE connection from the server, then waits for the page to reload from the session-expired notification on the new connection. EventSource accepts the response and the page reloads. Without the fix, the page never reloads and the test fails.
| verify(request, never()).setAttribute( | ||
| eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any()); | ||
| } else { | ||
| inOrder.verify(request).setAttribute( |
There was a problem hiding this comment.
🧪 The new test asserts that a request attribute was set on a mock, not that the notification is framed as an SSE event
CONVENTIONS.md (Testing): "Assert the observable outcome — DOM state, response, emitted event, public API result — not how it is produced. A test must fail when the behavior breaks and keep passing through a refactor that preserves it."
The resource is a Mockito mock, so no Atmosphere interceptor runs and nothing about the actual output is checked. The test passes whether or not Atmosphere still frames an unsuspended SSE write on this attribute, and it would fail for the alternative implementation discussed in the review (suspend and broadcast) even though that produces the same wire output. The only evidence that the fix works is the manual wire check in the PR discussion.
There are IdlePushChannel*IT variants for long polling and websocket but none for SSE, and none cover session expiry, so nothing in CI would catch a regression here.
PushHandlerTest.java:733 · tests
There was a problem hiding this comment.
Agreed. I replaced the mock-based test with SsePushSessionExpiredIT, which runs in the browser and checks the outcome: after the session is gone, the SSE reconnect must deliver the session-expired notification and the page must reload. It fails without the fix and passes with it. The IT runs in the Slow Tests job together with the other PushTests.
I also removed the WebSocket assertion added earlier in this PR, since it only checked the attribute too. The transport and suspended checks don't change the output on their own: only the SSE interceptor reads the attribute, and on a suspended SSE resource it already frames the write. So a test pinning those checks would be testing the implementation, not the behaviour. The PR description now says this.
Replace the unit test that only checked a request attribute on a mocked resource with an integration test: the server closes the SSE push connection after the session cookie is gone, and the page must reload from the session expired notification sent on the new connection. The test fails without the fix, as the client then keeps reconnecting. The websocket assertion added earlier in this PR is removed with it, as it also checked the attribute rather than the output.
| */ | ||
| @CustomPush(transport = Transport.SERVER_SENT_EVENTS) | ||
| @Route(value = "com.vaadin.flow.uitest.ui.push.SsePushReconnectView", layout = ViewTestLayout.class) | ||
| public class SsePushReconnectView extends BasicPushSseView { |
There was a problem hiding this comment.
Is there a similar test already for long polling and websocket that should be extended somehow, or are they done separately like this?
There was a problem hiding this comment.
No existing test covered this case for long polling or WebSocket. The closest were IdlePushChannelIT and BasicPushIT, but neither drops the session before the push connection reopens. So I followed the IdlePushChannelIT pattern: PushSessionExpiredIT is now abstract, with PushSessionExpiredLongPollingIT, PushSessionExpiredWebsocketIT and PushSessionExpiredSseIT subclasses using the existing BasicPush*Views. The button that closes the push connection from the server moved into BasicPushView, and the separate SSE-only view is gone. All three pass with the fix. Without it, only the SSE one fails.
There was a problem hiding this comment.
Does this require some update to the test migration doc?
There was a problem hiding this comment.
@Artur- This view is gone: its button now lives in the shared BasicPushView, and the test runs once per transport as PushSessionExpired{LongPolling,Sse,Websocket}IT.
MIGRATION.md needs no change. Transport is picked per test with @CustomPush, so this adds no new module permutation for the ledger.
MIGRATION_LIST.md did need an update, and I've made it. The four new ITs are now listed under push → test-default, next to the other push ITs in test-root-context, and the counts are updated. I added them by hand instead of rerunning the generator. Rerunning it would also pick up ITs that other PRs added but never listed (DeclaredJsImportIT, StringFallbackIT, QueuedHistoryUpdateIT, DevBundleJsDefinitionImportIT), and those don't belong in this PR.
Follow the IdlePushChannelIT pattern: an abstract test with one subclass per transport, so long polling and websocket are covered too. The button that closes the push connection from the server moves into BasicPushView, which all transport views extend, replacing the SSE-only SsePushReconnectView.
The new PushSessionExpired*IT classes live in test-root-context next to the other push ITs, so they move to test-default with the push package.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|
|
Documentation Bot: No documentation needed for this change. This is a bug fix ( Nothing was opened in vaadin/docs. If you disagree, the change has to be documented by hand there.
|
…) (CP: 25.4) Fixes #27189 **behavior deviation** · flow-server · apps using the experimental `SERVER_SENT_EVENTS` push transport With the SSE push transport, a tab that reconnects after its session expired or its UI is gone never got the session-expired notification or the refresh. The notification was written as a bare JSON line, which `EventSource` ignores, so the client kept reconnecting every few seconds until the next heartbeat (5 minutes by default) or until the browser reported going offline. The notification is now framed as an SSE event. Workaround before this fix: use the WebSocket or long-polling transport. **Risks:** -⚠️ Behaviour change: on SSE, a reconnect that is rejected now gets a `data:` event instead of a bare JSON line. Nothing could read the old reply. - ✅ No public API, security, memory, serialization or performance impact. Other transports and already open SSE connections are unchanged. **Context.** Atmosphere's SSE interceptor frames a write as an event only on a suspended resource, or when the request carries its `CALLBACK_JAVASCRIPT_PROTOCOL` attribute, which Atmosphere sets for the handshake of a new connection. The first connection was therefore already framed, but a reconnect carrying an existing tracking id was not. - On an SSE resource that is not suspended yet, `PushHandler` sets `CALLBACK_JAVASCRIPT_PROTOCOL` on the request before writing the notification, so Atmosphere adds the event framing, padding and message size prefix itself. - Only Atmosphere's SSE interceptor reads that attribute, and on a suspended SSE resource it already frames the write, so setting it for other transports or open connections would change nothing. The transport and suspended checks only keep the change to the case it fixes. - Added an integration test for each transport (long polling, WebSocket, SSE): the server closes the push connection after the session cookie is gone, and the page must reload from the session-expired notification sent on the new connection. Without the fix, the SSE variant keeps reconnecting and fails; the other two already passed and guard against regressions. The button that closes the push connection is added to the shared `BasicPushView`. It replaces the unit test added earlier, which only checked the attribute on a mocked resource. Working on behalf of `@mshabarov` --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com> Co-authored-by: Artur Signell <artur@vaadin.com>



Fixes #27189
behavior deviation · flow-server · apps using the experimental
SERVER_SENT_EVENTSpush transportWith the SSE push transport, a tab that reconnects after its session expired or its UI is gone never got the session-expired notification or the refresh. The notification was written as a bare JSON line, which
EventSourceignores, so the client kept reconnecting every few seconds until the next heartbeat (5 minutes by default) or until the browser reported going offline. The notification is now framed as an SSE event. Workaround before this fix: use the WebSocket or long-polling transport.Risks:
data:event instead of a bare JSON line. Nothing could read the old reply.Context. Atmosphere's SSE interceptor frames a write as an event only on a suspended resource, or when the request carries its
CALLBACK_JAVASCRIPT_PROTOCOLattribute, which Atmosphere sets for the handshake of a new connection. The first connection was therefore already framed, but a reconnect carrying an existing tracking id was not.PushHandlersetsCALLBACK_JAVASCRIPT_PROTOCOLon the request before writing the notification, so Atmosphere adds the event framing, padding and message size prefix itself.BasicPushView. It replaces the unit test added earlier, which only checked the attribute on a mocked resource.Working on behalf of
@mshabarov